<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
<meta name="google-site-verification" content="kYQuniuaRZuL9fq0DATfL7extAwcqWG6Ki286ZMcpak" />
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
<!-- 网站关键字  -->
  <meta name="keywords" content="web前端,前端博客,vue,js，html,css,html5,css3,node,express,git,react">
<!-- 网站作者  -->
  <meta name="author" content="wxb">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"blogwxb.cn","root":"/","scheme":"Gemini","version":"7.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="cookie、Local Storage、Session Storage 定义Cookie（局限性）：用户可以禁用cookie，最多只能存储4kb，cookie有过期时间的（一般我们设置的时间最长1个月，用户使用杀毒软件也可以清除我们的cookie），cookie还需要指定作用域，不可以跨域调用。 LocalStorage：持久化存储到本地，没有过期时间，最大可存储5MB都是采用的明文存储，我们在">
<meta property="og:type" content="article">
<meta property="og:title" content="JS 中cookie 、cookie的js插件、Local Storage、Session Storage">
<meta property="og:url" content="https://blogwxb.cn/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/index.html">
<meta property="og:site_name" content="会飞的猪的个人博客">
<meta property="og:description" content="cookie、Local Storage、Session Storage 定义Cookie（局限性）：用户可以禁用cookie，最多只能存储4kb，cookie有过期时间的（一般我们设置的时间最长1个月，用户使用杀毒软件也可以清除我们的cookie），cookie还需要指定作用域，不可以跨域调用。 LocalStorage：持久化存储到本地，没有过期时间，最大可存储5MB都是采用的明文存储，我们在">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blogwxb.cn/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/1.png">
<meta property="og:image" content="https://blogwxb.cn/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/2.png">
<meta property="og:image" content="https://blogwxb.cn/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/3.png">
<meta property="article:published_time" content="2020-03-28T10:38:46.000Z">
<meta property="article:modified_time" content="2020-03-28T11:03:08.181Z">
<meta property="article:author" content="wxb">
<meta property="article:tag" content="H5">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blogwxb.cn/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/1.png">

<link rel="canonical" href="https://blogwxb.cn/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>JS 中cookie 、cookie的js插件、Local Storage、Session Storage | 会飞的猪的个人博客</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?3bc21ab780489193ab1c6a33159920cd";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">会飞的猪的个人博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://blogwxb.cn/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="wxb">
      <meta itemprop="description" content="主要是分享自己在web前端的一些入的坑，脱的坑的经历，也会是分享一些node后端的知识。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="会飞的猪的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JS 中cookie 、cookie的js插件、Local Storage、Session Storage
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-03-28 18:38:46 / 修改时间：19:03:08" itemprop="dateCreated datePublished" datetime="2020-03-28T18:38:46+08:00">2020-03-28</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="cookie、Local-Storage、Session-Storage-定义"><a href="#cookie、Local-Storage、Session-Storage-定义" class="headerlink" title="cookie、Local Storage、Session Storage 定义"></a>cookie、Local Storage、Session Storage 定义</h1><p><font color="#f40">Cookie（局限性）</font>：用户可以禁用cookie，<font color="#f40">最多只能存储4kb</font>，cookie有过期时间的（一般我们设置的时间最长1个月，<font color="#f40">用户使用杀毒软件也可以清除我们的cookie</font>），cookie还需要指定作用域，不可以跨域调用。</p>
<p><font color="#f40">LocalStorage</font>：持久化存储到本地，没有过期时间，<font color="#f40">最大可存储5MB</font><br>都是采用的明文存储，我们在控制台（Resource）下可以看到存储的信息，所以本地存储都是不安全的（不要存储重要的信息，如果需要的话，我们需要对重要的信息进行严格的加密—md5加密）</p>
<p><font color="#f40">WebStorage(localStorage(常用，sessionStorage))</font><br>由于我们的localStorage可以存储很多的数据，我们在项目中把不经常更新的数据进行存储，下一次打开页面，先把本地缓存的数据展示，减少对于服务器的请求压力—移动端的本地缓存。</p>
<p><font color="#f40">sessionStorage</font>用于本地存储一个会话（session）中的数据，这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁。因此sessionStorage不是一种持久化的本地存储，仅仅是会话级别的存储。</p>
<a id="more"></a>
<h1 id="cookie"><a href="#cookie" class="headerlink" title="cookie"></a>cookie</h1><p>Cookie 以名/值对形式存储，如下所示:<br>username=John Doe</p>
<h2 id="使用-JavaScript-创建Cookie（增）"><a href="#使用-JavaScript-创建Cookie（增）" class="headerlink" title="使用 JavaScript 创建Cookie（增）"></a>使用 JavaScript 创建Cookie（增）</h2><p>JavaScript 中，<font color="#f40">创建 cookie</font> 如下所示：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie=<span class="string">"username=John Doe"</span>;</span><br></pre></td></tr></table></figure>
<p><font color="#f40">设置cookie的生命周期</font>。默认情况下，cookie 在浏览器关闭时删除，<font color="#f40">可以用max-age单位是秒</font>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie=<span class="string">"username=John Doe; max-age=5"</span>;<span class="comment">//cookie生命周期为5秒</span></span><br></pre></td></tr></table></figure>
<p>您可以使用<font color="#f40">path 参数告诉浏览器 cookie 的路径</font> 。默认情况下，cookie 属于当前页面。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie=<span class="string">"username=John Doe;max-age=5 ; path=/"</span>;</span><br></pre></td></tr></table></figure>

<h2 id="使用-JavaScript-删除-Cookie-删"><a href="#使用-JavaScript-删除-Cookie-删" class="headerlink" title="使用 JavaScript 删除 Cookie(删)"></a>使用 JavaScript 删除 Cookie(删)</h2><p>删除 cookie 非常简单。您只需要设置 max-age 参数为0即可，如下所示，设置为 max-age=0;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie = <span class="string">"username=;max-age=0;"</span>;</span><br></pre></td></tr></table></figure>
<p>注意，当您删除时不必指定 cookie 的值。</p>
<h2 id="使用-JavaScript-修改-Cookie（改）"><a href="#使用-JavaScript-修改-Cookie（改）" class="headerlink" title="使用 JavaScript 修改 Cookie（改）"></a>使用 JavaScript 修改 Cookie（改）</h2><p>在 JavaScript 中，修改 cookie 类似于创建 cookie，如下所示：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie=<span class="string">"username=John Smith; path=/"</span>;</span><br></pre></td></tr></table></figure>
<p>旧的 cookie 将被覆盖。</p>
<h2 id="JavaScript-读取-Cookie（查）"><a href="#JavaScript-读取-Cookie（查）" class="headerlink" title="JavaScript 读取 Cookie（查）"></a>JavaScript 读取 Cookie（查）</h2><p>在 JavaScript 中, 可以使用以下代码来读取 cookie，<font color="#f40">以字符串的形式返回 ，每对的cookie值都是以；结尾的，可以截取用</font><br>有些浏览器不识别汉字需要编码<br><font color="#f40">encodeURIComponent()</font>    对汉字进行编码<br><font color="#f40">decodeURIComponent()</font>　  对编码的结果进行解码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = <span class="built_in">document</span>.cookie;</span><br><span class="line"><span class="built_in">console</span>.log(x)</span><br></pre></td></tr></table></figure>
<p>运行结果<br><img src="/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/1.png" alt></p>
<p><img src="/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/2.png" alt></p>
<h1 id="轻量级JS-Cookie插件js-cookie的使用方法"><a href="#轻量级JS-Cookie插件js-cookie的使用方法" class="headerlink" title="轻量级JS Cookie插件js-cookie的使用方法"></a>轻量级JS Cookie插件js-cookie的使用方法</h1><p>介绍：js-cookie插件是一个JS操作cookie的插件，源文件只有3.34 KB，非常轻量级。js-cookie也支持npm和Bower安装和管理。</p>
<h2 id="引入js-cookie-js"><a href="#引入js-cookie-js" class="headerlink" title="引入js-cookie.js"></a>引入js-cookie.js</h2><ul>
<li><p>直接引用cdn：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=<span class="string">"https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>本地下载下来后：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=<span class="string">"/path/to/js.cookie.js"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>模块化开发时:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Cookies <span class="keyword">from</span> <span class="string">'js-cookie'</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="js-cookie-js常用的API和方法"><a href="#js-cookie-js常用的API和方法" class="headerlink" title="js-cookie.js常用的API和方法"></a>js-cookie.js常用的API和方法</h2><ol>
<li><p>设置cookie</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Cookies.set(<span class="string">'name'</span>, <span class="string">'value'</span>, &#123; max-age: <span class="number">7</span>, <span class="attr">path</span>: <span class="string">''</span> &#125;);<span class="comment">//7秒过期</span></span><br><span class="line">Cookies.set(<span class="string">'name'</span>, &#123; <span class="attr">foo</span>: <span class="string">'bar'</span> &#125;);<span class="comment">//设置一个json</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>读取cookie</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Cookies.get(<span class="string">'name'</span>);<span class="comment">//获取cookie</span></span><br><span class="line">Cookies.get(); <span class="comment">//读取所有的cookie</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>删除cookie</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Cookies.remove(<span class="string">'name'</span>); <span class="comment">//删除cookie时必须是同一个路径。</span></span><br></pre></td></tr></table></figure>


</li>
</ol>
<h1 id="localStorage和sessionStorage"><a href="#localStorage和sessionStorage" class="headerlink" title="localStorage和sessionStorage"></a>localStorage和sessionStorage</h1><p>localStorage和sessionStorage的语法是一样的</p>
<p>// 保存数据到sessionStorage</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sessionStorage.setItem(<span class="string">'key'</span>, <span class="string">'value'</span>);</span><br></pre></td></tr></table></figure>

<p>// 从sessionStorage获取数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> data = sessionStorage.getItem(<span class="string">'key'</span>);</span><br></pre></td></tr></table></figure>
<p>也可以直接用sessionStorage.key</p>
<p>// 从sessionStorage删除保存的数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sessionStorage.removeItem(<span class="string">'key'</span>);</span><br></pre></td></tr></table></figure>

<p>// 从sessionStorage删除所有保存的数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sessionStorage.clear();</span><br></pre></td></tr></table></figure>

<p>注意：<br>往localStorage和sessionStorage传值的话，比如传对象或者数组的时候记得先把其转换成json对象，用JSON.stringify();获取值的时候再用<font color="#f40">JSON.parse()或者eval(“(“+ JSON+”)”)</font>等方法去解析JSON。<br>例子：<br><img src="/2020/03/28/JS%20%E4%B8%ADcookie%20%E3%80%81cookie%E7%9A%84js%E6%8F%92%E4%BB%B6%E3%80%81Local%20Storage%E3%80%81Session%20Storage/3.png" alt></p>
<p>localStorage的语法和sessionStorage是一样的</p>
<p>关于cookie、localStorage和sessionStorage的区别详细的讲解<br><a href="https://www.cnblogs.com/pengc/p/8714475.html" target="_blank" rel="noopener">https://www.cnblogs.com/pengc/p/8714475.html</a></p>

    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="wxb 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="wxb 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/H5/" rel="tag"># H5</a>
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/03/21/js%E5%AF%B9%E8%B1%A1%E5%92%8C%E6%95%B0%E7%BB%84%E4%B9%8B%E9%97%B4%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2/" rel="prev" title="js对象和数组之间相互转换">
      <i class="fa fa-chevron-left"></i> js对象和数组之间相互转换
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/01/express%E4%B8%ADreq-session%E6%98%BE%E7%A4%BAundefined%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" rel="next" title="express中req.session显示undefined的解决方案">
      express中req.session显示undefined的解决方案 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#cookie、Local-Storage、Session-Storage-定义"><span class="nav-number">1.</span> <span class="nav-text">cookie、Local Storage、Session Storage 定义</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#cookie"><span class="nav-number">2.</span> <span class="nav-text">cookie</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#使用-JavaScript-创建Cookie（增）"><span class="nav-number">2.1.</span> <span class="nav-text">使用 JavaScript 创建Cookie（增）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用-JavaScript-删除-Cookie-删"><span class="nav-number">2.2.</span> <span class="nav-text">使用 JavaScript 删除 Cookie(删)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用-JavaScript-修改-Cookie（改）"><span class="nav-number">2.3.</span> <span class="nav-text">使用 JavaScript 修改 Cookie（改）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScript-读取-Cookie（查）"><span class="nav-number">2.4.</span> <span class="nav-text">JavaScript 读取 Cookie（查）</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#轻量级JS-Cookie插件js-cookie的使用方法"><span class="nav-number">3.</span> <span class="nav-text">轻量级JS Cookie插件js-cookie的使用方法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#引入js-cookie-js"><span class="nav-number">3.1.</span> <span class="nav-text">引入js-cookie.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#js-cookie-js常用的API和方法"><span class="nav-number">3.2.</span> <span class="nav-text">js-cookie.js常用的API和方法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#localStorage和sessionStorage"><span class="nav-number">4.</span> <span class="nav-text">localStorage和sessionStorage</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="wxb"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">wxb</p>
  <div class="site-description" itemprop="description">主要是分享自己在web前端的一些入的坑，脱的坑的经历，也会是分享一些node后端的知识。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">27</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">2</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/dearDreamWeb" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;dearDreamWeb" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/flyingwxb" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;flyingwxb" rel="noopener" target="_blank"><i class="gitee fa-fw"></i>Gitee</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">wxb</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div><script color="0,0,0" opacity="0.5" zIndex="-1" count="99" src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1/dist/canvas-nest.js"></script>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>















  

  

</body>
</html>
